import MessageSenderUtils from '../../../libs/message_sender_utils_0_0_3.js'
import Service from '../../../libs/service_0_0_6.js'
import DtoMessage from '../../../libs/dto/DtoMessage.js'
import UrlsStatistics from '../../../libs/dto/UrlsStatistics.js'

let urlsStatistics = new UrlsStatistics()

document.addEventListener('DOMContentLoaded', async function () {
	let buttonClearStatistics = document.querySelector('#button_clear_statistics')
    buttonClearStatistics.addEventListener('click', async function (event) {
        let tableData = document.querySelector('.table_data')
        tableData.innerHTML = ''
        urlsStatistics.clear()
    })
});

chrome.runtime.onMessage.addListener(
	function (request, sender, sendResponse) {
		let inMsg = JSON.parse(request)
		if (inMsg['recipient'] === utils.getCurrentName()) {
			utils.logInMessage(inMsg)
			let outMsg = new Object();
			outMsg.data = 'options OK'
			outMsg.sender = utils.getCurrentName()
			outMsg.recipient = 'devtools'
			utils.log('Отправка ответа по адресу: ' + outMsg.recipient)
			utils.logOutMessage(outMsg)
			sendResponse(JSON.stringify(outMsg));

            let jsonMsg = inMsg['data']
            let objMsg = DtoMessage.BuildFromJson(jsonMsg)

            urlsStatistics.addElement(objMsg.getUrl())
            let urlsStatisticsList = urlsStatistics.getStatistics()

			let tableData = document.querySelector('.table_data');
            let keys = Object.keys(urlsStatisticsList)
            tableData.innerHTML = ''
            // Цикл перебора блоков со значениями
            for (let i = 0; i < keys.length; i++) {
                let title = keys[i]

                let entry = urlsStatisticsList[keys[i]]
                let keysEntry = Object.keys(entry)

                let paramsColspan = new Object()
                paramsColspan['name'] = 'colspan'
                paramsColspan['value'] = 3
                let paramsStyle = new Object()
                paramsStyle['name'] = 'style'
                paramsStyle['value'] = 'background-color: #FFFFFF'

                let arrParams = new Array()
                arrParams.push(paramsColspan)
                arrParams.push(paramsStyle)
                let tdTitle = Service.createCustomElement('td', `${title}`, arrParams);

                // Создание строки с заголовком блока
                let trTitle = Service.createCustomElement('tr', ``, []);
                trTitle.append(tdTitle)
                tableData.append(trTitle)

                // Цикл перебора элементов списка
                for (let k = 0; k < keysEntry.length; k++) {
                    let innerEntry = entry[keysEntry[k]]
                    let value = innerEntry['url']
                    let count = innerEntry['count']

                    let trLine = Service.createCustomElement('tr', ``, []);
                    var tdTab = Service.createCustomElement('td', ' ', []);
                    trLine.append(tdTab)
                    var tdElemValue = Service.createCustomElement('td', `${value}`, []);
                    trLine.append(tdElemValue)
                    var tdElemCount = Service.createCustomElement('td', `${count}`, []);
                    trLine.append(tdElemCount)
                    tableData.append(trLine)
                }
            }
		} else {
			utils.logIgnoreMessage(inMsg)
		}
	}
);

let utils = new MessageSenderUtils('options')